package com.cangxuan.app.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
/*作者 田凯
*  图片转换为Base64编码
*
*   */

public class ImageToBase64Utils {
    public static String  change(String path) {
        String filePath = path;
        String base64String = convertImageToBase64(filePath);
        return base64String;
    }

    public static String convertImageToBase64(String filePath) {
        String base64String = "";
        File file = new File(filePath);
        try (FileInputStream imageInFile = new FileInputStream(file)) {
            // Reading a Image file from file system
            byte[] imageData = new byte[(int) file.length()];
            imageInFile.read(imageData);
            base64String = Base64.getEncoder().encodeToString(imageData);
        } catch (IOException e) {
            System.out.println("Exception while reading the image " + e);
        }
        return base64String;
    }
}